package com.resys.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.resys.exception.BusinessException;
import com.resys.utils.DateUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.resys.entity.enums.PageSize;
import com.resys.entity.query.UserQuery;
import com.resys.entity.po.User;
import com.resys.entity.vo.PaginationResultVO;
import com.resys.entity.query.SimplePage;
import com.resys.mappers.UserMapper;
import com.resys.service.UserService;
import com.resys.utils.StringTools;
import org.springframework.util.DigestUtils;


/**
 *  业务接口实现
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper<User, UserQuery> userMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<User> findListByParam(UserQuery param) {
		return this.userMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(UserQuery param) {
		return this.userMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<User> findListByPage(UserQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<User> list = this.findListByParam(param);
		PaginationResultVO<User> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(User bean) {
		return this.userMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<User> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.userMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<User> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.userMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(User bean, UserQuery param) {
		StringTools.checkParam(param);
		return this.userMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(UserQuery param) {
		StringTools.checkParam(param);
		return this.userMapper.deleteByParam(param);
	}

	/**
	 * 根据UserId获取对象
	 */
	@Override
	public User getUserByUserId(String userId) {
		return this.userMapper.selectByUserId(userId);
	}

	/**
	 * 根据UserId修改
	 */
	@Override
	public Integer updateUserByUserId(User bean, String userId) {
		return this.userMapper.updateByUserId(bean, userId);
	}

	/**
	 * 根据UserId删除
	 */
	@Override
	public Integer deleteUserByUserId(String userId) {
		return this.userMapper.deleteByUserId(userId);
	}

	/**
	 * 根据Username获取对象
	 */
	@Override
	public User getUserByUsername(String username) {
		return this.userMapper.selectByUsername(username);
	}

	/**
	 * 根据Username修改
	 */
	@Override
	public Integer updateUserByUsername(User bean, String username) {
		return this.userMapper.updateByUsername(bean, username);
	}

	/**
	 * 根据Username删除
	 */
	@Override
	public Integer deleteUserByUsername(String username) {
		return this.userMapper.deleteByUsername(username);
	}

	@Override
	public void register(String phone, String userName, String registerPassword,String realName,Integer role) {
		User user = new User();
		if(userMapper.selectByPhone(phone) != null){
			throw new BusinessException("该手机号已经被注册");
		}
		if(userMapper.selectByUsername(userName) != null){
			throw new BusinessException("该用户名已经被注册");
		}
		user.setPhone(phone);
		user.setUsername(userName);
		user.setPassword(DigestUtils.md5DigestAsHex(registerPassword.getBytes()));
		user.setCreatedAt(new Date());
		user.setAccountStatus(1);
		user.setRole(role);
		if(role==1) {
			user.setUserId("staff-" + StringTools.getRandomString(8));
		}
		if(role==2) {
			user.setUserId("custo-" + StringTools.getRandomString(8));
		}
		if(role==0){
			user.setUserId("admin-" + StringTools.getRandomString(8));
		}
		user.setRealName(realName);
		userMapper.insert(user);

	}


}